package com.xnx3.banmahuishou.userApplet.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.BaseVO;
import com.xnx3.banmahuishou.core.entity.Agency;
import com.xnx3.banmahuishou.core.entity.Daishoudian;
import com.xnx3.banmahuishou.core.entity.Wangge;
import com.xnx3.banmahuishou.userApplet.util.UserLocationUtil;
import com.xnx3.banmahuishou.userApplet.vo.DaishoudianListVO;
import com.xnx3.banmahuishou.userApplet.vo.IsLocationVO;
import com.xnx3.j2ee.controller.BaseController;
import com.xnx3.j2ee.entity.System;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.wangmarket.shop.core.entity.Order;
/**
 * 用户端--首页
 * @author qyw
 */
@Controller
@RequestMapping("/userApplet/daishoudian")
public class IntelligentDustbinController extends BaseController {

	@Resource
	private SqlService sqlService;


	/**
	 * 获取当前经纬度附近3km以内的代收点
	 * @param lat	纬度
	 * @param lon	经度
	 * @author qyw
	 */
	@RequestMapping(value="/getDaishoudian.json",method = {RequestMethod.POST})
	@ResponseBody
	public DaishoudianListVO getDaishoudian(HttpServletRequest request,
			@RequestParam(value = "lat", required = false,defaultValue = "0") double lat,
			@RequestParam(value = "lon", required = false,defaultValue = "0") double lon){
		
		System system = sqlService.findAloneByProperty(System.class, "name", "DAISHOUDIAN_FANWEI");
		String juli = "3.0";
		if (system != null) {
			juli = system.getValue();
		}
		// 获取当前经纬度附近3km以内的代收点,是审核通过的
		String str = "SELECT * FROM laji_daishoudian WHERE (st_distance(point(longitude,latitude),point( " 
				+ lon + "," + lat + ")) * 111195 / 1000) <= " + juli + " AND state = " + Daishoudian.STATE_APPROVED;
		List<Daishoudian> list = sqlService.findBySqlQuery(str, Daishoudian.class);
		DaishoudianListVO vo = new DaishoudianListVO();
		vo.setList(list);
		ActionLogUtil.insert(request, "获取当前经纬度附近" + juli + "km以内的代收点");
		return vo;
	}

	

	/**
	 * 用户端首页--获取订单最后五条作为用户首页滚动公告消息展示
	 * @author qyw
	 */
	@RequestMapping(value="/getOrderInfo.json",method = {RequestMethod.POST})
	@ResponseBody
	public List<String> getOrderInfo(HttpServletRequest request){
		System system = sqlService.findAloneByProperty(System.class, "name", "INDEX_ORDER_COUNT");
		String value = 200 + "";
		if (system != null) {
			value = system.getValue();
		}
		String string = "SELECT * FROM shop_order WHERE state = 3 OR state = 5 ORDER BY id DESC LIMIT 0," + value;
		List<Order> orders = sqlService.findBySqlQuery(string, Order.class);
		List<String> list = new ArrayList<String>();
		for (Order order : orders) {
			if (order.getSheng() != null && order.getShi() != null && order.getPhone() != null && order.getUsername() != null) {
				String username = order.getUsername().substring(0, 1) + "**";
				String phone = order.getPhone().substring(0, 3) + "****" + order.getPhone().substring(7);
				Double money = order.getPayMoney()/100.00;
				String info = order.getShi() + order.getQu() + " " + username + " " + phone + " 回收废品 收入" + money + "元";
				list.add(info);
			}
		}
		ActionLogUtil.insert(request, "用户端首页--获取订单最后" + value + "条数据作为用户首页滚动公告消息展示");
		return list;
	}
	
	
	
	
	/**
	 * 用户端首页--判断用户是否分配了代理商和网格
	 * @return  isNo 1:已分配 0:未分配
	 * @author qyw
	 */
	@RequestMapping(value="/isLocation.json",method = {RequestMethod.POST})
	@ResponseBody
	public IsLocationVO isLocation(HttpServletRequest request){

		IsLocationVO vo = new IsLocationVO();
		int userId = getUserId();
		User user = sqlService.findById(User.class, userId);
		if (user == null) {
			vo.setBaseVO(BaseVO.FAILURE, "请先登录");
			return vo;
		}
		// 是否分配了代理商和网格,1:已分配 0:未分配
		Integer isNo = 1;
		if (user.getDailishangUserid() == null || user.getDailishangUserid() <= 0) {
			isNo = 0;
		}
		if (user.getWanggeUserid() == null || user.getWanggeUserid() <= 0) {
			isNo = 0;
		}
		vo.setIsNo(isNo);
		vo.setUser(user);
		ActionLogUtil.insert(request, "用户端首页--判断用户是否分配了代理商和网格");
		return vo;
	}
	
	
	/**
	 * 用户端首页--将该用户分配到对应的代理商和网格
	 * @param sheng 	所在的省,如： 山东省
	 * @param shi 		所在的市，如 潍坊市
	 * @param qu 		所在的区，如 寒亭区
	 * @param lat		纬度
	 * @param lon		经度
	 * @author qyw
	 */
	@RequestMapping(value="/addUserLocation.json",method = {RequestMethod.POST})
	@ResponseBody
	public IsLocationVO addUserLocation(HttpServletRequest request,
			@RequestParam(value = "sheng", required = false, defaultValue = "") String sheng,
			@RequestParam(value = "shi", required = false, defaultValue = "") String shi,
			@RequestParam(value = "qu", required = false, defaultValue = "") String qu,
			@RequestParam(value = "lat", required = false,defaultValue = "0") double lat,
			@RequestParam(value = "lon", required = false,defaultValue = "0") double lon){
		
		IsLocationVO vo = new IsLocationVO();
		int userId = getUserId();
		User user = sqlService.findById(User.class, userId);
		if (user == null) {
			vo.setBaseVO(com.xnx3.j2ee.vo.BaseVO.NOT_LOGIN,"尚未登录");
			return vo;
		}
		// 判断是哪个代理商
		String string = "SELECT * FROM laji_agency WHERE state = 1 AND userid > 1";
		List<Agency> list = sqlService.findBySqlQuery(string, Agency.class);
		int dlsUserid = UserLocationUtil.getDlsUserid(sheng, shi, qu, list);
		user.setDailishangUserid(dlsUserid);
		// 判断是哪个网格
		string = "SELECT * FROM laji_wangge WHERE state = 1 AND dailishang_userid = " + dlsUserid;
		List<Wangge> wgList = sqlService.findBySqlQuery(string, Wangge.class);
		for (Wangge wangge : wgList) {
			Boolean b = UserLocationUtil.isWgLocation(lon, lat, wangge.getServiceArea());
			if (b) {
				user.setWanggeUserid(wangge.getId());
			}
		}
		user.setLongitude(lon);
		user.setLatitude(lat);
		// 保存用户信息
		sqlService.save(user);
		vo.setUser(user);
		// 更细缓存
		setUser(user);
		ActionLogUtil.insertUpdateDatabase(request,userId, "用户端首页--将该用户分配到对应的代理商和网格");
		return vo;
	}
	
	
	
	
	
	
	
	
}
